package com.runing.controller;


import com.runing.config.ResponseWrapper;
import com.runing.entity.Admin;
import com.runing.service.impl.AdminServiceImpl;
import com.runing.utils.fileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lel
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/login")
@Api(tags = "后台管理员")
public class AdminController {
    @Autowired
    AdminServiceImpl adminService;


    @ApiOperation("登录")
    @PostMapping("/DoLogin")
    public ResponseWrapper login(@RequestBody Admin admin){
        ResponseWrapper wrapper = adminService.login(admin);
        return wrapper;
    }

    @ApiOperation("显示管理员的全部信息和模糊查询")
    @GetMapping("/Admins")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每页展示的数据条数",defaultValue = "10"),
            @ApiImplicitParam(name = "pageNum",value = "页码数",defaultValue = "1"),
            @ApiImplicitParam(name = "aname",value = "管理员姓名")
    })
    public ResponseWrapper findAllAdmins(@RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,
                                         @RequestParam(name = "pageNum",defaultValue = "1")Integer pageNum,
                                         @RequestParam(name = "aname",required = false)String aname){
        ResponseWrapper wrapper = adminService.findAllAdmins(pageNum,pageSize,aname);
        return wrapper;
    }

    @ApiOperation("新增管理员")
    @PostMapping("/addAdmin")
    public ResponseWrapper addAdmin(@RequestBody Admin admin){
        ResponseWrapper wrapper = adminService.addAdmin(admin);
        return wrapper;
    }

    @ApiOperation("删除管理员,单个批量二合一,批量删除：1-2-3")
    @ApiImplicitParam(name ="aids",value = "1-2-3格式的id串")
    @DeleteMapping("/deleteAdmin")
    public ResponseWrapper deleteAdmin(@RequestParam(name = "aids")String aids){
        ResponseWrapper wrapper = adminService.deleteAdmin(aids);
        return wrapper;
    }

    @ApiOperation("修改管理员")
    @PutMapping("/updateAdmin")
    public ResponseWrapper updateAdmin(@RequestBody Admin admin){
        ResponseWrapper wrapper = adminService.updateAdmin(admin);
        return wrapper;
    }
    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aid",value = "管理员id"),
            @ApiImplicitParam(name = "oldpassword",value = "旧密码"),
            @ApiImplicitParam(name = "newpassword",value = "新密码")
    })
    @PutMapping("/updatePassword")
    public ResponseWrapper updatePassword(@RequestParam(name = "aid")Integer aid,
                                          @RequestParam(name = "oldpassword")String oldpassword,
                                          @RequestParam(name = "newpassword")String newpassword){
        ResponseWrapper wrapper = adminService.updatePassword(aid,oldpassword,newpassword);
        return wrapper;
    }
    /**
     上传文件的方法，将文件上传到某个存在的位置
     @param file 文件名
     */
    @PostMapping("/upload")
    public void upload(@RequestParam("file") MultipartFile file)throws Exception{
        // 这里由于逻辑较多所以封装了一个util
        fileUtil.upload(file);
    }

}

